﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.Converters
{
    public class CropStringConverter : IValueConverter
    {
        private const int CropLength = 75;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int cropLength = parameter == null ? CropLength : int.Parse(parameter.ToString());

            if (value.ToString().Length <= cropLength)
                return value.ToString();

            return value.ToString().Substring(0, cropLength) + "...";

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}